Ext.define('cloud.controller.Role', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            role: 'role',
            role_add: 'role #role_add',
            roleAdd: 'roleAdd',
            role_submit: 'roleAdd #role_submit',
            roleEdit: 'roleEdit',
            role_edit: 'roleEdit #role_edit',
            power: 'power',
            power_add: 'power #power_add',
            powerAdd: 'powerAdd',
            power_submit: 'powerAdd #power_submit',
            unPower_id: 'powerAdd checkboxfield[name=unPower_id]'
        },
        control: {
            role_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: "loadmask"});
                    var article = this.getRole();
                    var roleadd = this.getRoleAdd()||Ext.create('cloud.view.role.RoleAdd');
                    article.push(roleadd);
                    Ext.Viewport.unmask();
                }
            },

            'role list': {
                itemtap: function (list, index, element, record, target, event) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    if (list.id == 'roleList') {
                        if (target.target.name == 'power') {
                            var store = Ext.getStore('RolePurviewAllFind');
                            store.load({params: {rid: record.get('id')}});
                            this.getRole().push({xtype: 'power', title: record.get('name')});
                            Ext.getCmp('power_rid_show').setValue(record.get('id'))
                        } else {
                            //系统创建应用管理员不能进入编辑页
                            if(record.get('createtype')==0){
                                Ext.Viewport.unmask();
                                return false;
                            }else{
                                var roleEdit = this.getRoleEdit()||Ext.create('cloud.view.role.RoleEdit', {title: record.get('name')});
                                Ext.getCmp('role_id_edit').setValue(record.get('id'))
                                var form = this.getRole().push(roleEdit);
                                form.setRecord(record);
                            }
                        }
                    } else {
                        if (target.target.name == 'delet_power') {
                            Ext.Msg.confirm('确认删除？', '', function (data) {
                                if (data == 'yes') {
                                    Ext.Ajax.request({
                                        url: GLOBAL.api_url + 'role_purview_delete',
                                        params: {
                                            id: record.get('id')
                                        },
                                        scope: this,
                                        success: function (response) {
                                            Ext.getStore('RolePurviewAllFind').load({params: {rid: record.get('rid')}});
                                        },
                                        failure: function () {
                                            Ext.Msg.alert('服务器维护中...')
                                            return false;
                                        }
                                    })
                                }
                            }, this);
                        }
                    }
                    Ext.Viewport.unmask();
                }
            },

            role_submit: {
                tap: function () {
                    var role_name_add = Ext.getCmp('role_name_add').getValue();
                    if (Ext.isEmpty(role_name_add))
                        return Ext.Msg.alert('请输入角色名称');
                    Ext.Viewport.mask({xtype: 'loadmask'});

                    this.getRoleAdd().submit({
                        url: GLOBAL.api_url + 'app_role_add',
                        scope: this,
                        method: 'POST',
                        success: this.actionsuccess,
                        failure: this.actionfailure
                    });
                    Ext.Viewport.unmask();
                }
            },

            role_edit: {
                tap: function () {
                    var role_name_add = Ext.getCmp('role_name_edit').getValue();
                    if (Ext.isEmpty(role_name_add))
                        return Ext.Msg.alert('请输入角色名称');
                    Ext.Viewport.mask({xtype: 'loadmask'});

                    this.getRoleEdit().submit({
                        url: GLOBAL.api_url + 'app_role_update',
                        scope: this,
                        params: {
                            id: Ext.getCmp('role_id_edit').getValue()
                        },
                        method: 'POST',
                        success: this.actionsuccess,
                        failure: this.actionfailure
                    });
                    Ext.Viewport.unmask();
                }
            },

            power_add: {
                tap: function () {
                    var powerAdd = this.getPowerAdd();
                    if (powerAdd == null) {
                        powerAdd = Ext.create('cloud.view.role.PowerAdd');
                    }
                    var store=Ext.getStore('AreaStatusFindStore')
                    store.load({
                        params:{caid:localStorage.getItem('caid')
                        }, callback: function (data) {
                            if(data.length!=0){
                                var rid=Ext.getCmp('power_rid_show').getValue();
                                Ext.getCmp('power_rid_add').setValue(rid);
                                this.getRole().push(powerAdd);
                            }else{
                                Ext.Msg.alert('请先添加地区')
                            }
                        }, scope: this})
                }
            },
            power_submit: {
                tap: function () {
                    var rid = Ext.getCmp('power_rid_add').getValue()
                    var unPower_ids = this.getUnPower_id().getGroupValues();
                    if(unPower_ids==null||unPower_ids==''){
                        return Ext.Msg.alert('请选择权限');
                    }
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var cadid = Ext.getCmp('power_cadid_add').getValue();
                    Ext.Ajax.request({
                        url: GLOBAL.api_url + 'role_purview_add',
                        params: {
                            rid: rid,
                            afids: unPower_ids.join(','),
                            cadid: cadid
                        },
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            Ext.Msg.alert(result.Variables.Message.messagestr);
                            Ext.getStore('RolePurviewAllFind').load({params: {rid: rid}});
                            this.getRole().pop()
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            Ext.Msg.alert('服务器维护中...')
                            return false;
                        }
                    })

                    Ext.Viewport.unmask();
                }
            }
        }
    },
    actionsuccess: function (from, result) {
        Ext.Viewport.unmask();
        if (result.Variables.Result.code == 0) {
            var store = Ext.getStore('RoleAllFind');
            store.load({params: {caid: localStorage.getItem('caid')}});
            var role = this.getRole();
            role.pop()
        }
        Ext.Msg.alert(result.Variables.Message.messagestr)
    },
    actionfailure: function () {//请求失败时执行操作
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }
})
